package com.fruitshop.manager;

import com.fruitshop.manager.base.BaseManager;
import com.fruitshop.mapper.BuyRecordMapper;
import com.fruitshop.mapper.FruitMapper;
import com.fruitshop.mapper.UserMapper;
import com.fruitshop.model.BuyRecord;
import com.fruitshop.model.Command;
import com.fruitshop.model.Fruit;
import com.fruitshop.model.User;
import com.fruitshop.util.*;

import java.util.List;
import java.util.Optional;
import java.util.Scanner;
import java.util.stream.Collectors;

public class BuyRecordManager extends BaseManager {


    // 个人购买记录
    private static final String SELF = "1";

    // 全部
    private static final String ALL = "2";

    public BuyRecordManager(String title, List<Command> commandList) {
        super(title, commandList);
    }

    @Override
    protected String processCommand(String command) {
        switch (command) {

            case SELF:
                return showSelf();
            case ALL:
                return showAll();
        }
        return command;
    }

    private String showSelf() {

        setTitle("个人购买记录");
        printTitle();
        // 显示个人购买记录
        BuyRecordMapper mapper = DBUtil.getMapper(BuyRecordMapper.class);

        User user = UserInfoContext.getUser();
        List<BuyRecord> records = mapper.selectByUserId(user.getId());

        // 判断空
        if (CollectionUtils.isEmpty(records)) {
            PrintUtils.println("您还没有购买记录");
            ConsoleUtils.prompt(new Scanner(System.in), "任意按键退出");

            return NORMAL_EXIT;
        }


        //获取所有fruit id,stream
        List<Integer> fruitIds = records.stream().map(BuyRecord::getFruitId).distinct().collect(Collectors.toList());

        //获取所有水果信息
        List<Fruit> fruits = DBUtil.getMapper(FruitMapper.class).selectByIds(fruitIds);


        showFruitList(records, fruits, List.of(user));
            //按任意键退出
        Scanner scanner=new Scanner(System.in);
        ConsoleUtils.prompt(scanner, "任意按键退出");
        setTitle("购买记录");

        return NORMAL_EXIT;
    }

    private String showFruitList(List<BuyRecord> records,List<Fruit> fruits,List<User> users) {
        // 显示水果列表

        PrintUtils.printHighLight("用户ID\t\t用户名称\t\t水果ID\t\t水果名称\t\t购买数量\t\t总价格\t\t购买时间\t\t");
        PrintUtils.newLine();

        User user = null;

        Fruit fruit = null;
        for (BuyRecord record : records) {
            Optional<User> userOptional = users.stream().filter(u -> u.getId() == record.getUserId()).findFirst();
            if (!userOptional.isPresent()) {
                user = new User();
                user.setId(record.getUserId());
                user.setName("未知用户");
            }else{
                user = userOptional.get();
            }
            Optional<Fruit> fruitOptional = fruits.stream().filter(f -> f.getId() == record.getFruitId()).findFirst();
            if (!fruitOptional.isPresent()) {
                fruit = new Fruit();
                fruit.setId(record.getFruitId());
                fruit.setName("未知水果名称");
            }else{
                fruit = fruitOptional.get();
            }
            PrintUtils.println(record.getUserId() + "\t\t" + user.getName() + "\t\t" +fruit.getId() + "\t\t" + fruit.getName()+"\t\t"+record.getAmount()+"\t\t"+record.getPrice()+"元\t\t"+ DateUtils.formatDate(record.getBuyTime()) +"\t\t");
        }


        return NORMAL_EXIT;
    }

    private String showAll() {

        setTitle("全部购买记录");
        printTitle();
        // 显示所有购买记录
        BuyRecordMapper mapper = DBUtil.getMapper(BuyRecordMapper.class);
        List<BuyRecord> records = mapper.selectAll();

        // 判断空
        if (CollectionUtils.isEmpty(records)) {
            PrintUtils.println("没有购买记录");
            ConsoleUtils.prompt(new Scanner(System.in), "任意按键退出");
            return NORMAL_EXIT;
        }

        //获取所有userId，去重
        List<Integer> userIds = records.stream().map(BuyRecord::getUserId).distinct().collect(Collectors.toList());

        // 获取所有水果ID去重
        List<Integer> fruitIds = records.stream().map(BuyRecord::getFruitId).distinct().collect(Collectors.toList());

        // 获取所有用户信息
        List<User> users = DBUtil.getMapper(UserMapper.class).selectByIds(userIds);

        // 获取所有水果信息
        List<Fruit> fruits = DBUtil.getMapper(FruitMapper.class).selectByIds(fruitIds);

        showFruitList(records, fruits, users);

        //按任意键退出
        Scanner scanner=new Scanner(System.in);
        ConsoleUtils.prompt(scanner, "任意按键退出");
        setTitle("购买记录");

        return NORMAL_EXIT;
    }
}
